package com;

import java.util.ArrayList;
import java.util.Random;

public class Ant implements Cloneable, Comparable<Ant>{
	public int [] classesArray;
	private double quality;
	private double costAttribute;
	private double costMethod;
	public int numClasses;
	Random random = new Random();
	
	public int[] getClassesArray() {
		return classesArray;
	}

	public void setClassesArray(int[] classesArray) {
		this.classesArray = classesArray;
	}
	
	public int getNumClasses() {
		return this.numClasses;
	}

		
	public int getUltimaClasse() {
		int ultimaClasse = 0;
		for (int i = 0; i < this.classesArray.length; i++) {
			if(this.classesArray[i] != -1){
				ultimaClasse = this.classesArray[i];
			} else {
				break;
			}
		}
		return ultimaClasse;
	}
	
	public int sorteiaClasseNaoEscolhida() {
		ArrayList<Integer> classesNaoEscolhidas = new ArrayList<Integer>();
		int indiceEscolhido, classeEscolhida;
		
		for (int i = 1; i <= this.numClasses; i++) {
			if(!contemClasse(i)){
				classesNaoEscolhidas.add(i);
			}
		}
		
		if(classesNaoEscolhidas.size() == 1){
			return classesNaoEscolhidas.get(0);
		} else {
			indiceEscolhido = random.nextInt(classesNaoEscolhidas.size());
			classeEscolhida = classesNaoEscolhidas.get(indiceEscolhido); 
			return classeEscolhida;
		}
	}
	
	public ArrayList<Integer> getClassesNaoEscolhidas() {
		ArrayList<Integer> classesNaoEscolhidas = new ArrayList<Integer>();
		
		for (int i = 1; i <= this.numClasses; i++) {
			if(!contemClasse(i)){
				classesNaoEscolhidas.add(i);
			}
		}
		return classesNaoEscolhidas;
	}
	
	public boolean contemClasse(int classe){
		for (int i = 0; i < this.classesArray.length; i++) {
			if(classe == this.classesArray[i]){
				return true;
			}
		}
		return false;
	}
	
	public Ant(int numClasses){
		this.numClasses = numClasses;
		classesArray = new int[numClasses + 1];
		for(int x=0; x <= numClasses; x++){
			classesArray[x] = -1;
		}
	}

	public void clearclassesArray(){
		for(int x=0; x < classesArray.length; x++)
			classesArray[x] = -1;
	}
	
		
	public boolean hasRules(){
		boolean hasRules=false;
		for(int x=0; x < classesArray.length && !hasRules; x++)
			if(classesArray[x] != -1)
				hasRules = true;
		return hasRules;
	}
	
	public Object clone() throws CloneNotSupportedException{
		Ant antClone = new Ant(this.numClasses);
		antClone.setClassesArray((int[])this.classesArray.clone());
		antClone.setCostAttribute(this.costAttribute);
		antClone.setCostMethod(this.costMethod);
		antClone.setQuality(this.quality);
		return antClone;
	}

	public int compareTo(Ant o) {
		// TODO Auto-generated method stub
		return new Double(getQuality()).compareTo(new Double(o.getQuality()));
	}

	public double getQuality() {
		return quality;
	}

	public void setQuality(double quality) {
		this.quality = quality;
	}

	public double getCostAttribute() {
		return costAttribute;
	}

	public void setCostAttribute(double costAttribute) {
		this.costAttribute = costAttribute;
	}

	public double getCostMethod() {
		return costMethod;
	}

	public void setCostMethod(double costMethod) {
		this.costMethod = costMethod;
	}

}

